﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lhxm.Sgs.IBll;
using Lhxm.Sgs.Bll;
using Lhxm.Sgs.Model;
using Lhxm.Sgs.Model.EnumType;

namespace Lhxm.Sgs.WebApp.Controllers
{
    public class UserAccountController : Controller
    {
        //IUserAccountService userAccountSr = new UserAccountService();
        //运用spring .net 代替以上的new()
        IUserAccountService userAccountSr { get; set; }
        //
        // GET: /UserAccount/

        public ActionResult Index()
        {
            return View();
        }
        
        public ActionResult GetUserAccountList()
        {
            int pageIndex = Request["page"]!=null?int.Parse(Request["page"]):1;
            int pageSize = Request["rows"] != null ? int.Parse(Request["rows"]) : 1;
            int total;
            //short delFlag = (short)DelEnumType.LogDel;    
            //var list = userAccountSr.LoadPageEntities<int>(pageIndex,pageSize,out total,C=> C.email != null ,C => C.id,true);
            var list = userAccountSr.LoadPageEntities<int>(pageIndex, pageSize, out total, C => true, C => C.id, true);

            var temp = from u in list
                       select new
                       {
                           userName = u.userName,
                           userId = u.id,
                           userPwd = u.password,
                           userEmail = u.email,
                           userPhone = u.shouji,
                           userWX = u.weixin
                       };
            return Json(new { rows=temp,total=total});
            //C => C.email IS NOT NULL
        }

        public ActionResult DeleteRec()
        {
            string strId = Request["strId"];
            string[] strIds = strId.Split(',');
            List<int> list = new List<int>();
            foreach (string id in strIds)
            {
                list.Add(Convert.ToInt32(id));
            }
            //根据编号删除数据==调用业务层
            if (userAccountSr.DeleteRecs(list))
            {
                return Content("ok");
            }
            else
            {
                return Content("no");
            }
        }

        #region 添加用户数据
        public ActionResult AddUserAccount(userAccount userAccount)
        {
            userAccountSr.AddEntity(userAccount);
            return Content("ok");
        }
        #endregion

        #region 展示要修改的用户数据
        public ActionResult ShowEditInfo()
        {
            int id = int.Parse(Request["id"]);
            var userAccount=userAccountSr.LoadEntities(u=>u.id==id).FirstOrDefault();
            return Json(userAccount,JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 修改的用户数据
        public ActionResult EditUserAccount(userAccount userAccount)
        {
            if (userAccountSr.EditEntity(userAccount))
            {
                return Content("ok");
            }
            else
            {
                return Content("fail");
            }
        }
        #endregion
    }
}
